#include <vector>
#include <math.h>
#include <string>
#include <fstream>
#include <cstdlib>
#include <stdio.h>
#include <iomanip>
#include <iostream>
#include <stdlib.h>

#include "genetic_lego/dssim.h"
#include "genetic_lego/GeneticLegoApp.h"




using namespace Wt;
using namespace std;

//Forward Declarations


WApplication *createApplication(const WEnvironment& env)
{
  GeneticLegoApp *app = new GeneticLegoApp( env );
  return app;
}

#include <cv.h>
#include <highgui.h>
#include "genetic_lego/GeneticEvaluation.h"

int genetic_test()
{
  using namespace GeneticEvaluation;

//  const char *original = "test_images/einstein.png";
//  const char *original = "/Users/wjohnson/Desktop/ItalianJen0_smaller.jpg";
//  const char *original = "/Users/wjohnson/Desktop/jen_coll.jpg";
  const char *original = "/Users/wcjohns/Desktop/Lenna1024.png";
  GenOptData data( original );

  data.npopulation    = 10000;
  data.maxgenerations = 100;
  data.pixels_per_cell = 10;
  data.nlegos = 0.25 * (data.npixel_x/data.pixels_per_cell)
                     * (data.npixel_y/data.pixels_per_cell);
  data.color_and_type_sigma = 1.5;
  data.position_mutation_sigma = 10.0;
  data.lego_colors_only = false;

  cout << "Starting optization for " << data.npixel_x << "x" << data.npixel_y
       << " image, using " << data.nlegos << " loegos" << endl;
  perform_genetic_optimization( data );

  return 0;
}//int genetic_test()


#include <Wt/WPainter>
#include <Wt/WSvgImage>
#include <Wt/WRasterImage>
#include "genetic_lego/LegoRenderer.h"
#include "genetic_lego/LegoDescription.h"

int lego_test()
{
  boost::shared_ptr<LegoSceneDescription> scene( new LegoSceneDescription() );

  scene->scene_width = 600;
  scene->scene_height = 400;
  scene->pixels_per_cell = 25;

  LegoDescription lego;
  lego.set_color( LegoDescription::kRed );
  lego.type = LegoDescription::k2x4;

  lego.pos_x = 5;
  lego.pos_y = 5;

  scene->legos.push_back( lego );

  lego.set_color( LegoDescription::kBlue );
  scene->legos.push_back( lego );


//   Wt::WRasterImage pngImage("png", 600, 400);
   Wt::WSvgImage pngImage(600, 400);

   {
      LegoRenderer::paintLegoScene( &pngImage, scene );
   }

   std::ofstream f("lego.svg", std::ios::out | std::ios::binary);
   pngImage.write(f);
//   return 1;

   CvSize imgSize;
   imgSize.width = scene->scene_width;
   imgSize.height = scene->scene_height;

   IplImage *img = cvCreateImage( imgSize,IPL_DEPTH_8U,3);
   LegoRenderer::paintLegoScene( img, scene );
   cvSaveImage( "cvLego.png" ,img );

//  const char *file1 = "test_images/einstein.png";
//  const char *file2 = "test_images/blur.png";
//  double ds = dssim( file1, file2 );
   //  cout << "ds=" << ds << endl;
//  assert(0);
  return 1;
}//lego_test()



int main( int argc, char *argv[] )
{
//  return lego_test();
  return genetic_test();

  return WRun( argc, argv, &createApplication );
}//int main(int argc, char *argv[])

